//
//  WLLocalNotification.m
//  WanLian
//
//  Created by 123456 on 2019/1/15.
//  Copyright © 2019 wanlian. All rights reserved.
//

#import "WLLocalNotification.h"

#import <UserNotifications/UserNotifications.h>
@implementation WLLocalNotification
+ (void)postLocalNotificationWithAlertBody:(NSString *)alertBody {
    // 1.创建通知
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    // 2.设置通知的必选参数
    // 设置通知显示的内容
    localNotification.alertBody = alertBody;
    // 设置通知的发送时间,单位秒
//    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    //解锁滑动时的事件
//    localNotification.alertAction = @"知道了!";
    //收到通知时App icon的角标
//    localNotification.applicationIconBadgeNumber = 1;
    //推送是带的声音提醒，设置默认的字段为UILocalNotificationDefaultSoundName
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    // 3.发送通知(🐽 : 根据项目需要使用)
    // 方式一: 根据通知的发送时间(fireDate)发送通知
//    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    
    // 方式二: 立即发送通知
     [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

}
//创建本地通知
-(void) requestNotification
{
    if (@available(iOS 10, *))
    {
        UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
            
            if (granted) {
                // 允许推送
            }else{
                //不允许
            }
            
        }];
    }
    else if(@available(iOS 8 , *))
    {
        UIApplication * application = [UIApplication sharedApplication];
        
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
        [application registerForRemoteNotifications];
    }
    else
    {
        UIApplication * application = [UIApplication sharedApplication];
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
        [application registerForRemoteNotifications];
    }
}
+ (void)checkCurrentNotificationStatusWithBlock:(void (^)(BOOL hasAuthor))resBlock
{
    if (@available(iOS 10 , *))
    {
        [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (settings.authorizationStatus == UNAuthorizationStatusDenied)
                {
                    if (resBlock) {
                        resBlock(NO);
                    }
                } else {
                    if (resBlock) {
                        resBlock(YES);
                    }
                }
            });
            
            
        }];
    }
    else if (@available(iOS 8 , *))
    {
        UIUserNotificationSettings * setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
        
        if (setting.types == UIUserNotificationTypeNone) {
            if (resBlock) {
                resBlock(NO);
            }
        } else {
            if (resBlock) {
                resBlock(YES);
            }
        }
    }
    else
    {
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (type == UIUserNotificationTypeNone)
        {
            if (resBlock) {
                resBlock(NO);
            }
        } else {
            if (resBlock) {
                resBlock(YES);
            }
        }
    }
}

@end
